package org.okcjug.december.comparator;

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import org.junit.Test;
import org.okcjug.december.model.Movie;
import org.okcjug.december.util.MovieFactory;

public class SerializationTest {
	@Test
	public void comparatorSerializationTest() throws IOException {
		TreeMap<Movie, List<Integer>> movieAndTimes = new TreeMap<Movie, List<Integer>>(new LargestMovieTitleComparator());
		TreeMap<Movie, List<Integer>> movieAndTimesSerializedComparator = new TreeMap<Movie, List<Integer>>(new LargestMovieTitleSerializedComparator());
		List<Integer> movieTimes = new ArrayList<Integer>();
		movieTimes.add(1);
		movieTimes.add(4);
		movieTimes.add(6);
		movieAndTimes.put(MovieFactory.getClerks(), movieTimes);
		movieAndTimesSerializedComparator.put(MovieFactory.getClerks(), movieTimes);
		movieAndTimes.put(MovieFactory.getMadMaxThunderDome(), movieTimes);
		movieAndTimesSerializedComparator.put(MovieFactory.getMadMaxThunderDome(), movieTimes);
		movieAndTimes.put(MovieFactory.getPatton(), movieTimes);
		movieAndTimesSerializedComparator.put(MovieFactory.getPatton(), movieTimes);
		
		try{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(movieAndTimes);
			oos.close();
			baos.close();
			fail("Should have failed");
		}catch(NotSerializableException e){
			//do nothing should happen
		}
		
		try{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(movieAndTimesSerializedComparator);
			oos.close();
			baos.close();
		}catch(NotSerializableException e){
			fail(e.getMessage());
		}		
	}
}
